{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "be4b8527",
   "metadata": {},
   "source": [
    "# Caffe Input 层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eb6b4782",
   "metadata": {
    "tags": [
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "from tvm_book.config import env\n",
    "# 设置 caffeprotobuf环境\n",
    "env.set_caffeproto(Path(env.__file__).parents[3]/\"tests/caffeproto\")\n",
    "# 设置tvm环境\n",
    "env.set_tvm(\"/media/pc/data/board/arria10/lxw/tasks/tvm-test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4275596d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "from google.protobuf import text_format\n",
    "import caffe_pb2 as pb2\n",
    "\n",
    "temp_dir = Path(\".temp\")\n",
    "temp_dir.mkdir(exist_ok=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7989e73",
   "metadata": {},
   "source": [
    "## Caffe Input 层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "adea4fee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name: \"data\"\n",
       "type: \"Input\"\n",
       "top: \"data\"\n",
       "input_param {\n",
       "  shape {\n",
       "    dim: 1\n",
       "    dim: 1\n",
       "    dim: 120\n",
       "    dim: 120\n",
       "  }\n",
       "}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = \"\"\"\n",
    "name: \"data\"\n",
    "type: \"Input\"\n",
    "top: \"data\"\n",
    "input_param {\n",
    "shape: { dim: 1 dim: 1 dim: 120 dim: 120 }\n",
    "}\n",
    "\"\"\".strip()\n",
    "pl = text_format.Merge(text, pb2.LayerParameter())\n",
    "pl"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a876c14",
   "metadata": {},
   "source": [
    "将 caffe Input 算子转换为 Relax 算子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7779861d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"highlight\" style=\"background: \"><pre style=\"line-height: 125%;\"><span></span>data\n",
       "</pre></div>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from tvm.relax.testing import nn\n",
    "dtype = \"float32\"\n",
    "\n",
    "shape = pl.input_param.shape\n",
    "assert len(shape)==1 and len(pl.top)==1, \"Input 类型仅仅支持单输入单输出\"\n",
    "shape = list(shape[0].dim)\n",
    "x = nn.Placeholder(shape, dtype, pl.name)\n",
    "x.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "084dc928",
   "metadata": {},
   "source": [
    "## Caffe 多输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5b7f0be3",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"\"\"\n",
    "name: \"多输入\"\n",
    "layer {\n",
    "  name: \"data1\"\n",
    "  type: \"Input\"\n",
    "  top: \"data1\"\n",
    "  input_param {\n",
    "    shape: { dim: 1 dim: 1 dim: 120 dim: 240 }\n",
    "  }\n",
    "}\n",
    "\n",
    "layer {\n",
    "  name: \"data2\"\n",
    "  type: \"Input\"\n",
    "  top: \"data2\"\n",
    "  input_param {\n",
    "    shape: { dim: 1 dim: 3 dim: 32 dim: 64 }\n",
    "  }\n",
    "}\n",
    "\"\"\".strip()\n",
    "predict_net = text_format.Merge(text, pb2.NetParameter())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c5791596",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"highlight\" style=\"background: \"><pre style=\"line-height: 125%;\"><span></span>data1\n",
       "</pre></div>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"highlight\" style=\"background: \"><pre style=\"line-height: 125%;\"><span></span>data2\n",
       "</pre></div>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from tvm.relax.testing import nn\n",
    "dtype = \"float32\"\n",
    "\n",
    "for pl in predict_net.layer:\n",
    "    shape = pl.input_param.shape\n",
    "    assert len(shape)==1 and len(pl.top)==1, \"Input 类型仅仅支持单输入单输出\"\n",
    "    shape = list(shape[0].dim)\n",
    "    x = nn.Placeholder(shape, dtype, pl.name)\n",
    "    x.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py313",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
